package com.jd.fruit.controller.mobile;


import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.MD5;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.PropKit;

@Controller("/mobile/weixin")
public class WeiXinController extends BaseController{

	private String unifyPayApiUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder?";
	
	//统一支付接口
	public void unifyPayApi(){
		String appid = PropKit.get("WeiXinAppId");
		String mch_id = PropKit.get("WeiXinShangHuId");
		String weixinPaySecert = PropKit.get("WeiXinShangHuPaySecret");
		String notifyUrl = PropKit.get("weixinNotiyUrl");
		
		String device_info = "123";
		String nonce_str = StringUtil.getKey();
		String body ="Test GOODS";
		String out_trade_no = "100001";
		String total_fee = "0.1";
		String spbill_create_ip ="127.0.0.1";
		String trade_type = "JSAPI";
		String auth_code="123456";
		String sign = this.getSign(appid, auth_code, body, device_info, mch_id, nonce_str, out_trade_no, spbill_create_ip, total_fee, weixinPaySecert);

		this.setAttr("unifyPayApiUrl", unifyPayApiUrl);
		this.setAttr("appid",appid);
		this.setAttr("mch_id",mch_id);
		this.setAttr("device_info",device_info);
		this.setAttr("nonce_str",nonce_str);
		this.setAttr("sign",sign);
		this.setAttr("body",body);
		this.setAttr("out_trade_no",out_trade_no);
		this.setAttr("total_fee",total_fee);
		this.setAttr("spbill_create_ip",spbill_create_ip);
		this.setAttr("notify_url",notifyUrl);
		this.setAttr("trade_type",trade_type);
		this.render("/mobile/weixin/weixin_unify_pay.html");
	}
	
	public void notifyUrl(){
		this.renderHtml("Hello!");
	}
	
	public void getPayConfigJson(){
//		PropKit.get("token");
		
//		String timeStamp = new Date().getTime()/1000 + "";
//		String nonceStr = StringUtil.getKey();
//		String packageStr = "";
//		
		
	}
	
	private String getSign(String appid,String auth_code,String body,String device_info,String mch_id,String nonce_str,String out_trade_no,
			String spbill_create_ip,String total_fee,String weixinPaySecert){
		StringBuffer signUrl = new StringBuffer();
		signUrl.append("appid=").append(appid);
		signUrl.append("&auth_code=").append(auth_code);
		signUrl.append("&body=").append(body);
		signUrl.append("&device_info=").append(device_info);
		signUrl.append("&mch_id=").append(mch_id);
		signUrl.append("&nonce_str=").append(nonce_str);
		signUrl.append("&out_trade_no=").append(out_trade_no);
		signUrl.append("&spbill_create_ip=").append(spbill_create_ip);
		signUrl.append("&key=").append(weixinPaySecert);
		String sign = MD5.md5(signUrl.toString()).toUpperCase();
		return sign;
	}
	
	
}
